Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to tagged Struct types #677

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add support to tagged Struct types #677

wants to merge 2 commits into from

Conversation

flejz
Copy link

@flejz flejz commented Jun 26, 2024

One use case that me and my coworkers found is that we would often be loggin the same fields from a Struct over and over again, so we came up with the idea of logging tagged structs. That's what this PR is about.

That introduces a new concept of tagging the Struct with log followed by the log field name as the example below:

type User struct {
  Name   string `log:"name"`
  Active bool   `log:"is_active"`
}

Usage

We are introducing a new logging method Struct where you can pass the struct as an interface parameter.

user := User {
  Name:   "dude",
  Active: true,
}

log.Log().Struct(user).Msg("")

That would generate the log result as follows:

{"name":"dude","is_active":true}

PS: Ommiting the log tag will omit the struct field from the logs
PS2: Nested struct fields are also supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant